<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/3
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;
/**
 * 押金退还💰👈🏼
 * 
 */
class DepositReturn extends Tpmecms
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 押金退款查询全部
     *
     * @return void
     */
    public function deposit_return_query_all()
    {
        $deposit_return_time = Config::get('site.deposit_return_time');

        $order_status = '1';
        $db = $this->GetTableByOrderStatus($order_status);
        $s = 0;//发单人
        $j = 0;//接单人
        $order_list = Db::name($db)
            ->where('status',6)//状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            ->where('spay_status',1)//发货人支付押金:1=已支付,2=未支付,3=已违约
            ->where('jpay_status',1)//接单人支付押金:1=已支付,2=未支付,3=已违约
            ->select();
        foreach ($order_list as $key => $val) {
            if($val['sdeposit_status']!=2 && $val['s_money']>0){
                $res = $this->DepositReturn($val['s_uid'],$val['s_money'],$val['id'],1,$order_status,3);
                if($res)
                    $up_status = Db::name($db)->where('id',$val['id'])->update(['sdeposit_status'=>'2']);
                    if($up_status)
                        $s++;
            }

            if($val['jdeposit_status']==1 && $val['j_money']>0){ //接单人押金退款:1=待退,2=已退,3=退还给货主

                switch ($val['deposit_status']) {//押金退款:1=退还给司机,2=退还给发货人
                    // case '1':
                    //     $res = $this->DepositReturn($val['j_uid'],$val['j_money'],$val['id'],2,$order_status,3);
                    //     if($res){
                    //         $up_status = Db::name($db)->where('id',$val['id'])->update(['jdeposit_status'=>'2']);
                    //         if($up_status)
                    //             $j++;
                    //     }
                           
                    //     break;
                    case '2':
                        $res = $this->DepositReturn($val['s_uid'],$val['j_money'],$val['id'],2,$order_status,3,2);//只能为3钱包
                        if($res){
                            $up_status = Db::name($db)->where('id',$val['id'])->update(['jdeposit_status'=>'3']);
                            if($up_status)
                                $j++;
                        }
                        break;
                    default:
                        $res = $this->DepositReturn($val['j_uid'],$val['j_money'],$val['id'],2,$order_status,3);
                        if($res){
                            $up_status = Db::name($db)->where('id',$val['id'])->update(['jdeposit_status'=>'2']);
                            if($up_status)
                                $j++;
                        }
                        break;
                }
                
           
            }
        }
        $json[$db] = ['发单人退款数'=>$s,'接单人退款数'=>$j];




        //____________________________________________________________________________________________________________
        //____________________________________________________________________________________________________________


        $order_status = '2';
        $db = $this->GetTableByOrderStatus($order_status);
        $s = 0;//发单人
        $j = 0;//接单人
        $order_list = Db::name($db)
            ->where('status',6)//状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            ->where('spay_status',1)//发货人支付押金:1=已支付,2=未支付,3=已违约
            ->where('jpay_status',1)//接单人支付押金:1=已支付,2=未支付,3=已违约
            ->select();
        foreach ($order_list as $key => $val) {
            if($val['sdeposit_status']!=2 && $val['s_money']>0){
                $res = $this->DepositReturn($val['s_uid'],$val['s_money'],$val['id'],1,$order_status,3);
                if($res)
                    $up_status = Db::name($db)->where('id',$val['id'])->update(['sdeposit_status'=>'2']);
                    if($up_status)
                        $s++;
            }

            if($val['jdeposit_status']!=2 && $val['j_money']>0){
                $res = $this->DepositReturn($val['j_uid'],$val['j_money'],$val['id'],2,$order_status,3);
                if($res)
                    $up_status = Db::name($db)->where('id',$val['id'])->update(['jdeposit_status'=>'2']);
                    if($up_status)
                        $j++;
            }
        }
        $json[$db] = ['发单人退款数'=>$s,'接单人退款数'=>$j];




        //____________________________________________________________________________________________________________
        //____________________________________________________________________________________________________________

        $order_status = '4';
        $db = $this->GetTableByOrderStatus($order_status);
        $s = 0;//发单人
        $j = 0;//接单人
        $order_list = Db::name($db)
            ->where('status',6)//状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            ->where('spay_status',1)//发货人支付押金:1=已支付,2=未支付,3=已违约
            ->where('jpay_status',1)//接单人支付押金:1=已支付,2=未支付,3=已违约
            ->select();
        foreach ($order_list as $key => $val) {
            if($val['sdeposit_status']!=2 && $val['s_money']>0){
                $res = $this->DepositReturn($val['s_uid'],$val['s_money'],$val['id'],1,$order_status,3);
                if($res)
                    $up_status = Db::name($db)->where('id',$val['id'])->update(['sdeposit_status'=>'2']);
                    if($up_status)
                        $s++;
            }

            if($val['jdeposit_status']!=2 && $val['j_money']>0){
                $res = $this->DepositReturn($val['j_uid'],$val['j_money'],$val['id'],2,$order_status,3);
                if($res)
                    $up_status = Db::name($db)->where('id',$val['id'])->update(['jdeposit_status'=>'2']);
                    if($up_status)
                        $j++;
            }
        }
        $json[$db] = ['发单人退款数'=>$s,'接单人退款数'=>$j];



        //____________________________________________________________________________________________________________
        //____________________________________________________________________________________________________________

        halt($json);



    }

    /**
     * 退押金
     *
     * @param int $uid 用户ID
     * @param string $money 金额
     * @param int $order_id 订单id
     * @param string $role_status 角色:1=货主,2=司机
     * @param string $order_status 订单类型:1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @param string $pay_type 退款类型:1=微信,2=支付宝,3=钱包,9=其他
     * @param string $deposit_status 押金退款:1=退还给司机,2=退还给发货人
     * @return void
     */
    protected function DepositReturn($uid,$money,$order_id,$role_status,$order_status,$pay_type=null,$deposit_status=null)
    {
        
        switch ($pay_type) {
            case '3':
                return $this->DepositReturnToUserMoney($uid,$money,$order_id,$role_status,$order_status,$deposit_status);
                break;
            
            default:
                //不传则退到原支付上
                return $this->PayOrderDepositReturn();
                break;
        }

    }



    /**
     * 退押金到用户钱包
     *
     * @param string $uid 用户ID
     * @param string $money 金额
     * @param string $order_id 订单id
     * @param string $role_status 角色:1=货主,2=司机
     * @param string $order_status 订单类型:1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @return void
     */
    protected function DepositReturnToUserMoney($uid,$money,$order_id,$role_status,$order_status,$deposit_status=null)
    {
        
        $role = [1 =>'货主' , 2 =>'司机'];
        $OrderStatus = [1=>'普通发货',2=>'城市快运',3=>'拉货搬家',4=>'顺风车'];

        $data['order'] = $this->rand_order('Depos'.$role_status,$uid,26);
        $data['money'] = $money;
        $data['order_id'] = $order_id;
        $data['order_status'] = $order_status;
        $data['uid'] = $uid;
        $data['role_status'] = $role_status;
        $data['time'] = time();
        $data['pay_type'] = 3;//退款类型:1=微信,2=支付宝,3=钱包,9=其他
        $data['pay_time'] = time();
        $data['pay_money'] = $money*100;

        Db::name('deposit_return_order')->insert($data);
        //插入钱包
        $ps = $OrderStatus[$order_status].$role[$role_status].'押金退还';
        if($deposit_status==2){
            $ps = $OrderStatus[$order_status].$role[$role_status].'押金退还给发货人';
        }
        $this->IncUserMoney($uid,$money,$ps,$order_id);
        return true;
    }

    
    /**
     * 退押金到原支付上
     *
     * @param string $uid 用户ID
     * @param string $money 金额
     * @param string $order_id 订单id
     * @param string $role_status 角色:1=货主,2=司机
     * @param string $order_status 订单类型:1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @return void
     */
    protected function PayOrderDepositReturn($uid,$money,$order_id,$role_status,$order_status)
    {
        
        $role = [1 =>'货主' , 2 =>'司机'];
        $OrderStatus = [1=>'普通发货',2=>'城市快运',3=>'拉货搬家',4=>'顺风车'];

        $data['order'] = $this->rand_order('Depos'.$role_status,$uid,26);
        $data['money'] = $money;
        $data['order_id'] = $order_id;
        $data['order_status'] = $order_status;
        $data['uid'] = $uid;
        $data['role_status'] = $role_status;
        $data['time'] = time();
        $data['pay_type'] = 3;//退款类型:1=微信,2=支付宝,3=钱包,9=其他
        $data['pay_time'] = time();
        $data['pay_money'] = $money*100;

        Db::name('deposit_return_order')->insert($data);
        //退款

        return false;
    }
}